package com.boot.unity.api.Constroller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.common.Result;
import com.boot.unity.api.constants.NotifyType;
import com.boot.unity.api.utils.ConstantSmsUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "用于前端给用户发送短信验证码")
@RestController
@RequestMapping("/send")
@RequiredArgsConstructor
public class SendMessageController {

//    @GetMapping("/code")
    @PostMapping("/code")
    public Result sendCode(@RequestBody Map<String,String> body){
        System.out.println(body);
        String phone = body.get("phone");
        String code = body.get("code");
//        System.out.println(phone);
        ConstantSmsUtils smsUtils = ConstantSmsUtils.getInstance();
        smsUtils.send(phone, NotifyType.CAPTCHA,new String[]{code});

        return Result.success();
    }



    @PostMapping("/getCode")
    public Result sendGetCode(@RequestBody String body){
        JSONObject json = JSONObject.parseObject(body);
        String phone = json.getString("phone");
        String code = json.getString("code");

        ConstantSmsUtils smsUtils = ConstantSmsUtils.getInstance();
        smsUtils.send(phone, NotifyType.CAPTCHA,new String[]{code});
        return Result.success();
    }
}
